package com.tongfusoft.document.web.securityc.service.impl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Service;

import com.tongfusoft.document.manager.user.service.IDocumentUserService;
import com.tongfusoft.utils.Constant;

@Service("customAuthenticationSuccessHandler")
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler   {

	@Autowired
	@Qualifier(value="DocumentUserService")
	public IDocumentUserService  userService;
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
		session.setAttribute(Constant.SESSION_USER, userService.getUserByLoginName(userDetails.getUsername()));
		response.sendRedirect(request.getContextPath()+"/");
	}

}
